package org.offsync.util

import java.nio.ByteBuffer

class Window[T](
    protected[offsync] val array: Array[T],
    protected[offsync] val start: Int,
    val length: Int) {
  assert(start >= 0)
  assert(start + length <= array.length)
  
  def from(
      offset: Int): Window[T] = {
    assert(offset >= 0)
    assert(start + offset <= length)
    return new Window[T](array, start + offset, length - offset)
  }

  def until(
      offset: Int): Window[T] = {
    assert(offset >= 0)
    assert(offset <= length)
    return new Window[T](array, start, offset)
  }
  
  def apply(
      index: Int): T= {
    assert(index >= 0)
    assert(index < length)
    return array(start + index)
  }
  
  def toByteBuffer()(implicit ev: Array[T] <:< Array[Byte]) = ByteBuffer.wrap(array, start, length)
}

object Window {
  def to[T](
      array: Array[T]): Window[T] = new Window[T](array, 0, array.length)
}
